package mobile.map;


import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import mobile.map.FHMinOverlay;
import mobile.map.FHMinLocationListener;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import mobile.map.R;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

/**
 * @author  Dave
 */
public class FHMinMapActivity extends MapActivity {

	LinearLayout linearLayout;
	/**
	 * @uml.property  name="mapView"
	 * @uml.associationEnd  
	 */
	static FHMinMapView mapView;

	private List<Overlay> mapOverlays;
	Drawable drawable;
	Drawable shLayout;
	MyLocationOverlay lOverlay;

	private FHMinOverlay droidOverlays;
	boolean overlaysEnabled = true;
	boolean viewFH = false;
	/**
	 * @uml.property  name="locationListener"
	 * @uml.associationEnd  
	 */
	private FHMinLocationListener locationListener = new FHMinLocationListener();
	private LocationManager locationManager;
	
	private List<OverlayItem> myLocations = new ArrayList<OverlayItem>() ;
	private List<GeoPoint> myGeoPoints = new ArrayList<GeoPoint>() ;
	
	TypedArray roomNames = null;
	
	@Override
	public void onPause() {
		super.onPause();
	    locationManager.removeUpdates(locationListener);
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mapView = (FHMinMapView) findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(false);

		mapOverlays = mapView.getOverlays();
		
		Resources res = getResources();

		drawable = res.getDrawable(R.drawable.map_pin);
		droidOverlays = new FHMinOverlay(drawable, this);
		
		defineRoomNames();
		
		drawMapOverlays();		

		// Acquire a reference to the system Location Manager
		locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);		

		// Define a listener that responds to location updates
		locationListener = new FHMinLocationListener(this, myGeoPoints, myLocations);		
		
		lOverlay = new MyLocationOverlay(getBaseContext(), mapView);
		lOverlay.enableMyLocation();
		mapOverlays.add(lOverlay);

		// Register the listener with the Location Manager to receive location updates
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
		
		final Button WhereAmIButton = (Button) findViewById(R.id.announceLocation);
		WhereAmIButton.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				locationListener.announceLocation();
			}
		});
		
	}

	// The dispatchTouchEvent below is included to prevent user's with
	// multi-touch enabled devices from zooming out using multi-touch
	//
	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		if (ev.getPointerCount() > 1) {
			// We will not process multi-touch events
			return false;
		} else {
			return super.dispatchTouchEvent(ev);
		}    	
	}

	//make sure no route shows up on the screen on start up
	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.map_menu, menu);
	    return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	    case R.id.toggleDroids:
	    	ToggleDroidOverlays();
	        return true;
	    case R.id.changeSites:
	    	ToggleSitesInViewer();
	        return true;
	    default:
	        return super.onOptionsItemSelected(item);
	    }
	}

	public void ToggleDroidOverlays() {	
		if (overlaysEnabled) {
			//turn the droids off
			mapOverlays.remove(droidOverlays);
			overlaysEnabled = false;
		} else {
			//they're off, so turn them back on
			mapOverlays.add(droidOverlays);
			overlaysEnabled = true;
		}
		//refresh the mapview using postInvalidate to make the above
		//changes take effect
		mapView.postInvalidate();
	}
	
	public void ToggleSitesInViewer() {
		mapView.changeSites();
		defineRoomNames();
		drawMapOverlays();
		mapView.postInvalidate();
	}
	
	private void defineRoomNames() {
		if (mapView.currentSite == 1) {
			roomNames = getResources().obtainTypedArray(R.array.FHMinRoomNames);
		} else {
			roomNames = getResources().obtainTypedArray(R.array.SHRoomNames);
		}
	}
	
	private void drawMapOverlays() {
		
		@SuppressWarnings("rawtypes")
		Class arrayRes = R.array.class;
		Field arrayResField = null;
		int arrayResId = 0;
		
		myGeoPoints.clear();
		myLocations.clear();
		

		for (int i = 0; i < roomNames.length(); i++) {
			try {
				arrayResField = arrayRes.getField(roomNames.getString(i).replaceAll(" ", ""));
			} catch (SecurityException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (NoSuchFieldException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			try {
				arrayResId = arrayResField.getInt(null);
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			myGeoPoints.add(new GeoPoint(getResources().getIntArray(arrayResId)[0], getResources().getIntArray(arrayResId)[1]));
			myLocations.add(new OverlayItem(myGeoPoints.get(i), roomNames.getString(i),roomNames.getString(i)));
		}
		mapOverlays.remove(droidOverlays);
		droidOverlays.clear();
		for (int i = 0; i < myLocations.size(); i++) {
			droidOverlays.addOverlay(myLocations.get(i));
		}
		
		if (overlaysEnabled) {
			mapOverlays.add(droidOverlays);
		}		
	}
}